﻿using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

namespace MessageCenterAlerts
{
    public abstract partial class AlertsSettings : ModuleSettingsBase
    {
        public override void LoadSettings()
        {
            try
            {
                if (IsPostBack) return;

                var strEnablePopupForMessages = (string)Settings["EnablePopupForMessages"];
                bool enablePopupForMessages;
                if (string.IsNullOrEmpty(strEnablePopupForMessages) || !bool.TryParse(strEnablePopupForMessages, out enablePopupForMessages))
                    ckEnableForMessages.Checked = Alerts.Defaults.EnablePopupForMessages;
                else
                    ckEnableForMessages.Checked = enablePopupForMessages;

                var strEnablePopupForNotifications = (string)Settings["EnablePopupForNotifications"];
                bool enablePopupForNotifications;
                if (string.IsNullOrEmpty(strEnablePopupForNotifications) || !bool.TryParse(strEnablePopupForNotifications, out enablePopupForNotifications))
                    ckEnableForNotifications.Checked = Alerts.Defaults.EnablePopupForNotifications;
                else
                    ckEnableForNotifications.Checked = enablePopupForNotifications;

                var strEnableUpdateOfUsersControl = (string)Settings["EnableUpdateOfUsersControl"];
                bool enableUpdateOfUsersControl;
                if (string.IsNullOrEmpty(strEnableUpdateOfUsersControl) || !bool.TryParse(strEnableUpdateOfUsersControl, out enableUpdateOfUsersControl))
                    ckEnableUsersUpdate.Checked = Alerts.Defaults.EnableUpdateOfUsersControl;
                else
                    ckEnableUsersUpdate.Checked = enableUpdateOfUsersControl;

                var strMilisecondsUntilInitalDisplay = (string)Settings["MilisecondsUntilInitalDisplay"];
                int milisecondsUntilInitalDisplay;
                if (string.IsNullOrEmpty(strMilisecondsUntilInitalDisplay) || !int.TryParse(strMilisecondsUntilInitalDisplay, out milisecondsUntilInitalDisplay) || milisecondsUntilInitalDisplay < 0)
                    ntbSecondsUntilDisplay.Value = Alerts.Defaults.MilisecondsUntilInitalDisplay / 1000.0;
                else
                    ntbSecondsUntilDisplay.Value = milisecondsUntilInitalDisplay / 1000.0;

                var strMilisecondsBetweenChecks = (string)Settings["MilisecondsBetweenChecks"];
                int milisecondsBetweenChecks;
                if (string.IsNullOrEmpty(strMilisecondsBetweenChecks) || !int.TryParse(strMilisecondsBetweenChecks, out milisecondsBetweenChecks) || milisecondsBetweenChecks < 1000)
                    ntbSecondsBetweenChecks.Value = Alerts.Defaults.MilisecondsBetweenChecks / 1000.0;
                else
                    ntbSecondsBetweenChecks.Value = milisecondsBetweenChecks / 1000.0;

                var strMilisecondsToDisplay = (string)Settings["MilisecondsToDisplay"];
                int milisecondsToDisplay;
                if (string.IsNullOrEmpty(strMilisecondsToDisplay) || !int.TryParse(strMilisecondsToDisplay, out milisecondsToDisplay) || milisecondsToDisplay < 2000)
                    ntbSecondsToDisplay.Value = Alerts.Defaults.MilisecondsToDisplay / 1000.0;
                else
                    ntbSecondsToDisplay.Value = milisecondsToDisplay / 1000.0;

                var effect = (string)Settings["Effect"];
                if (string.IsNullOrEmpty(effect))
                    effect = Alerts.Defaults.Effect;

                var selectedEffect = ddlEffect.Items.FindByText(effect);
                if (selectedEffect != null)
                    selectedEffect.Selected = true;


                var strEffectDuration = (string)Settings["EffectDuration"];
                int effectDuration;
                if (string.IsNullOrEmpty(strEffectDuration) || !int.TryParse(strEffectDuration, out effectDuration) || effectDuration < 0)
                    ntbEffectDuration.Value = Alerts.Defaults.EffectDuration / 1000.0;
                else
                    ntbEffectDuration.Value = effectDuration / 1000.0;
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                var objModules = new ModuleController();

                objModules.UpdateTabModuleSetting(TabModuleId, "EnablePopupForMessages", ckEnableForMessages.Checked.ToString());
                objModules.UpdateTabModuleSetting(TabModuleId, "EnablePopupForNotifications", ckEnableForNotifications.Checked.ToString());
                objModules.UpdateTabModuleSetting(TabModuleId, "EnableUpdateOfUsersControl", ckEnableUsersUpdate.Checked.ToString());
                objModules.UpdateTabModuleSetting(TabModuleId, "MilisecondsUntilInitalDisplay", ntbSecondsUntilDisplay.Value.HasValue ? (ntbSecondsUntilDisplay.Value * 1000).ToString() : "");
                objModules.UpdateTabModuleSetting(TabModuleId, "MilisecondsBetweenChecks", ntbSecondsBetweenChecks.Value.HasValue ? (ntbSecondsBetweenChecks.Value * 1000).ToString() : "");
                objModules.UpdateTabModuleSetting(TabModuleId, "MilisecondsToDisplay", ntbSecondsToDisplay.Value.HasValue ? (ntbSecondsToDisplay.Value * 1000).ToString() : "");
                objModules.UpdateTabModuleSetting(TabModuleId, "EffectDuration", ntbEffectDuration.Value.HasValue ? (ntbEffectDuration.Value * 1000).ToString() : "");
                objModules.UpdateTabModuleSetting(TabModuleId, "Effect", ddlEffect.SelectedValue);
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
    }
}